global class NA_RegisteredSubscriptions
{
    // A class to accept as an input records (e.g. data combinations)
    global class rsInputs
    {
        webservice integer RowId;
        webservice String State;
        webservice String Referrer;
        webservice String Code;
        webservice integer CodeVersion;
        webservice integer Status;
        webservice String Email;
        webservice String FirstOrderRef;
        webservice String SubscriptionRef;
        webservice integer Duration;
        webservice String SubscriptionURL;
        webservice String SubscriptionDate;
        webservice String XMLContent;
    }
 
    // A class to send back as an output
	global class rsOutputs
	{
		webservice boolean resultFlag;
	}
 
    // The actual web service method we will call
	webservice static rsOutputs RegisteredSubscriptionsRequest(rsInputs inputs)
	{
		String queryTxt = '';
		rsOutputs output = new rsOutputs();
		output.resultFlag = false;
		if(inputs==null){ return output;}
		
		output.resultFlag = true;
		
		inputs.Referrer = AT_Utils.parseURL(inputs.Referrer);
		
		Account accObj = null;
		if((inputs.Referrer!=null && inputs.Referrer!='') || 
			(inputs.Email!=null && inputs.Email!=''))
		{
			
			if(inputs.Referrer!=null && inputs.Referrer!='') queryTxt = 'Referrer__c=\''+inputs.Referrer+'\'';
			if(inputs.Email!=null && inputs.Email!='')
			{ 
				if(queryTxt!='') queryTxt +=' OR ';  
				queryTxt += 'PersonEmail=\''+inputs.Email+'\'';
			}
			queryTxt  = 'select ID from Account where '+queryTxt+' limit 1';
			system.debug('>>>>>>>>>>>> SQL: '+queryTxt);
			for(Account item:Database.query(queryTxt))
			{
				accObj = item;
			}
		}
		
		System.debug('>>>>>>>>>>>>>>> inputs.State: '+inputs.State);
		//for(Account)
		
		if(inputs.State=='Activated')
		{
			Subscriptions__c subscrItem = new Subscriptions__c();
			if(accObj!=null) subscrItem.Account__c = accObj.id;
			
			try{ subscrItem.Creation_Date__c = date.valueOf(inputs.SubscriptionDate);}
	    	catch(Exception ex){ subscrItem.Creation_Date__c = null;}
	    	subscrItem.Duration__c = inputs.Duration;
	    	subscrItem.Email__c = inputs.Email;
	    	subscrItem.First_Order_Reference__c = inputs.FirstOrderRef;
	    	
	    	subscrItem.Referrer__c = inputs.Referrer;
	    	subscrItem.Row_ID__c = inputs.RowId;
	    	subscrItem.Serial_Number__c = inputs.Code;
	    	subscrItem.Serial_Number_Version__c = inputs.CodeVersion;
	    	
	    	subscrItem.State__c = inputs.State;
	    	subscrItem.Status__c = inputs.Status;
	    	subscrItem.Subscription_Reference__c = inputs.SubscriptionRef;
	    	subscrItem.Subscription_URL__c = inputs.SubscriptionURL;
	    	subscrItem.Subscription_XML__c = inputs.XMLContent;
	    	
	    	try{ insert subscrItem;}
	    	catch(Exception ex){ output.resultFlag = false;}
		}
		else if(inputs.State=='Canceled' || inputs.State=='Changed')
		{
			Subscriptions__c subscrItem = null;
			if((inputs.RowId!=null) || 
				(inputs.Referrer!=null && inputs.Referrer!=''))
			{
				queryTxt = '';
				if(inputs.RowId!=null) queryTxt = 'Row_ID__c='+inputs.RowId;
				if(inputs.Referrer!=null && inputs.Referrer!='')
				{ 
					if(queryTxt!='') queryTxt +=' AND ';  
					queryTxt = 'Referrer__c=\''+inputs.Referrer+'\'';
				}
				queryTxt  = 'select ID,State__c,Subscription_XML__c from Subscriptions__c where '+queryTxt+' limit 1';
				for(Subscriptions__c item:Database.query(queryTxt))
				{
					subscrItem = item;
				}
			}
			
			if(subscrItem != null)
			{
				subscrItem.State__c = inputs.State;
				//subscrItem.Subscription_XML__c = inputs.XMLContent;

				update subscrItem;
			}
		}
		
		

		return output;
	}

}